﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using FrameWork.web.Code;
using FrameWork.Entity;
using FrameWork.Components;

namespace FrameWork.web.Module.ITMS.Service.ReliefInfo
{
    public partial class ReliefInfo : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                sys_UserTable ut = BusinessFacade.sys_UserDisp(Common.Get_UserID);
                this.labUSERNAME.Text = ut.U_CName;
                this.labUSERNO.Text = ut.U_UserNO;
                this.txtRELIEFDAT.Text = DateTime.Now.AddHours(-12).ToShortDateString();
                bindReliefLog();
            }
        }
        /// <summary>
        /// 绑定交接班信息
        /// </summary>
        private void bindReliefLog()
        {
            its_ReliefLog rl = new its_ReliefLog();
            rl.USERNO = this.labUSERNO.Text;
            its_ReliefLogCs rlCs = new its_ReliefLogCs();
            ArrayList arr = rlCs.ReliefLog_Select(rl, this.PUReliefInfo.CurrentPageIndex,this.PUReliefInfo.PageSize);
            DataTable dtSource= arr[1] as DataTable;
            foreach (DataRow dr in dtSource.Rows)
            {
                dr["CONTENT"] = dr["CONTENT"].ToString().Replace("\r\n", "<br/>");
                if (dr["CONTENT"].ToString().Substring(0, 5) == "[未处理]")
                {
                    dr["CONTENT"] = "<font color='red'>" + dr["CONTENT"].ToString() + "</font>";
                }
            }
            this.GVReliefInfo.DataSource = dtSource;
            this.GVReliefInfo.DataBind();
            this.PUReliefInfo.RecordCount = (int)arr[0];

        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (this.txtRELIEFDAT.Text.Trim() == "")
            {
                this.ClientScript.RegisterStartupScript(this.GetType(), "NoDate", "<script>alert('请填写交接班时间！');</script>");
                return;
            }
            its_ReliefLog rl = new its_ReliefLog();
            rl.USERNAME = this.labUSERNAME.Text;
            rl.USERNO = this.labUSERNO.Text;
            rl.RELIEFDAT = (DateTime)Common.sink(txtRELIEFDAT.UniqueID, MethodType.Post, 50, 0, DataType.Dat);
            rl.RELIEFNO = int.Parse(this.rdbRELIEFNO.SelectedValue);
            rl.CONTENT = this.txtCONTENT.Text.Trim();
            its_ReliefLogCs rlCs = new its_ReliefLogCs();
            rlCs.ReliefLog_Add(rl);
            EventMessage.MessageBox(1, "保存成功", "交接班保存成功!", Icon_Type.OK, this.Request.Url.ToString());
            try
            {
                this.txtCONTENT.Text = "";
                bindReliefLog();
            }
            catch (Exception ex)
            {
                this.ClientScript.RegisterStartupScript(this.GetType(), "Error", "<script>alert('保存失败，请联系管理员！');</script>");
                FileTxtLogs.WriteLog(ex.Message);
            }


        }

        protected void btnEdit_Click(object sender, EventArgs e)
        {
            foreach (GridViewRow gr in this.GVReliefInfo.Rows)
            {
                
                if (int.Parse(((Label)gr.FindControl("labUID")).Text) == int.Parse(this.txtEditUID.Text))
                {
                    labEditUSERNO.Text = ((HyperLink)gr.FindControl("labUSERNO")).Text;
                    labEditUSERNAME.Text = ((HyperLink)gr.FindControl("labUSERNAME")).Text;
                    rdbEditRELIEFNO.SelectedValue = ((Label)gr.FindControl("labRELIEFNO")).Text;
                    txtEditRELIEFDAT.Text = ((Label)gr.FindControl("labRELIEFDAT")).Text.Replace("年","-").Replace("月","-").Replace("日","");
                    labEditFILLINDAT.Text = ((Label)gr.FindControl("labFILLINDAT")).Text.Replace("年", "-").Replace("月", "-").Replace("日", "");
                    txtEditCONTENT.Text = ((Label)gr.FindControl("labCONTENT")).Text.Replace("<br/>","\r\n").Replace("<font color='red'>","").Replace("</font>","");
                    if (labEditUSERNO.Text.Trim() != this.labUSERNO.Text.Trim())
                    {
                        btnEditSave.Enabled = false;
                    }
                    else
                    {
                        btnEditSave.Enabled = true;
                    }
                    break;
                }
            }
            this.ClientScript.RegisterStartupScript(this.GetType(), "Edit", "<script>showTabEdit();</script>");
        }

        protected void PUReliefInfo_PageChanged(object sender, EventArgs e)
        {
            bindReliefLog();
        }

        protected void btnEditSave_Click(object sender, EventArgs e)
        {
            its_ReliefLog rl = new its_ReliefLog();
            rl.UID = int.Parse(this.txtEditUID.Text);
            rl.USERNO = this.labEditUSERNO.Text;
            rl.USERNAME = this.labEditUSERNAME.Text;
            rl.RELIEFNO = int.Parse(this.rdbEditRELIEFNO.SelectedValue);
            rl.RELIEFDAT = (DateTime)Common.sink(txtEditRELIEFDAT.UniqueID, MethodType.Post, 50, 0, DataType.Dat);
            //DateTime.Parse(txtEditRELIEFDAT.Text);
            rl.FILLINDAT = DateTime.Parse(labEditFILLINDAT.Text);
            rl.CONTENT = txtEditCONTENT.Text;
            its_ReliefLogCs rlCs = new its_ReliefLogCs();
            rlCs.ReliefLog_Update(rl);
            bindReliefLog();
        }

        protected void btnCreate_Click(object sender, EventArgs e)
        {
            string strMeg="";
            if(this.txtRELIEFDAT.Text=="")
            {
                strMeg+="请输入交班时间！";
            }
            if(strMeg != "")
            {
                 this.ClientScript.RegisterStartupScript(this.GetType(), "Edit", "<script>alert('"+strMeg+"');</script>");
                return;
            }
            string strWhere="where 1=1 ";

            string strBegin = "";
            string strEnd = "";
            DateTime dtm = DateTime.Parse(txtRELIEFDAT.Text);
            if (this.rdbRELIEFNO.SelectedValue == "0")
            {
                strBegin = dtm.ToShortDateString() + " 8:00:00";
                strEnd = dtm.ToShortDateString() + " 20:00:00";
            }

            if (this.rdbRELIEFNO.SelectedValue == "1")
            {
                strBegin = dtm.AddDays(-1).ToShortDateString() + " 20:00:00";
                strEnd = dtm.ToShortDateString() + " 8:00:00";
            }
            if (this.rdbRELIEFNO.SelectedValue == "2")
            {
                strBegin = dtm.AddDays(-1).ToShortDateString() + " 8:00:00";
                strEnd = dtm.ToShortDateString() + " 8:00:00";
            }
            strWhere=strWhere+" and DocDat>='"+strBegin+"' and DocDat<='"+strEnd+"' ";
           string strUserID= Common.Get_UserID.ToString();
           strWhere += " and AllotPSNID=" + strUserID;
            AllSeach _AllSeach = new AllSeach();
            DataTable dt = _AllSeach.SupesoftPage("view_ServiceDocSeach", "ServDocNo,RepContent,DocDat,case Statue when 0 then '[未处理]' when 1 then '[已处理]' end as Statue", int.MaxValue, 1, strWhere, "Statue", "0").Tables[0];
            string strValue = "";
            int i = 1;
            foreach (DataRow dr in dt.Rows)
            {

                strValue += dr["Statue"].ToString() + "  " + i.ToString() + "、" + dr["ServDocNo"].ToString() + "：" + dr["RepContent"].ToString() + "\r\n";

                i++;
            }
            this.txtCONTENT.Text = strValue;
          
        }

       
    }
}
